package com.ruoyi.accounting.factory;

import com.ruoyi.accounting.service.AccountingRecordStrategy;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流水策略工厂
 */
@Component
public class AccountRecordStrategyFactory {
    @Resource
    private List<AccountingRecordStrategy> strategyList = new ArrayList<>();

    private final Map<Integer, AccountingRecordStrategy> strategies = new HashMap<>();

    @PostConstruct
    public void init() {
        for (AccountingRecordStrategy strategy : strategyList) {
            strategies.put(strategy.getType(), strategy);
        }
    }

    public AccountingRecordStrategy getStrategy(Integer type) {
        AccountingRecordStrategy strategy = strategies.get(type);
        if (strategy == null) {
            throw new ServiceException("无法获取流水处理策略: " + type);
        }

        return strategy;
    }
}
